package com.cskaoyan.service.wx.address;

import com.cskaoyan.bean.MarketAddress;
import com.cskaoyan.bean.MarketAddressExample;
import com.cskaoyan.bean.vo.wx.address.AddressOfListVo;
import com.cskaoyan.mapper.MarketAddressMapper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author Mr.Tang
 * @since 2022/09/09 23:34
 */
@Service
public class AddressServiceImpl implements  AddressService {

    @Autowired
    MarketAddressMapper marketAddressMapper;


    /**
     * 收货地址列表
     * @return addressOfListVO
     * 2022年9月10日00:00:36
     */
    @Override
    public AddressOfListVo listForAddress(Integer userId) {
        // 生成一个相应对象做数据封装
        AddressOfListVo addressOfListVo = new AddressOfListVo();
        // 备用（没有使用）
        MarketAddress marketAddress = new MarketAddress();
        // 新建一个查询语句
        MarketAddressExample marketAddressExample = new MarketAddressExample();
        MarketAddressExample.Criteria criteria = marketAddressExample.createCriteria();
        // 通过当前用户id查询显示逻辑删除为false的地址信息
        criteria.andUserIdEqualTo(userId);
        criteria.andDeletedEqualTo(false);
        List<MarketAddress> marketAddresses = marketAddressMapper.selectByExample(marketAddressExample);
        // 分页，将信息放入相应对象中，返回响应数据
        PageInfo<MarketAddress> marketAddressPageInfo = new PageInfo<>(marketAddresses);

        addressOfListVo.setTotal((int) marketAddressPageInfo.getTotal());
        addressOfListVo.setPages(marketAddressPageInfo.getPages());
        addressOfListVo.setLimit(marketAddressPageInfo.getPageSize());
        addressOfListVo.setList(marketAddressPageInfo.getList());


        return addressOfListVo;
    }


    /**
     * 编辑界面显示信息
     * @param id
     * @return
     */
    @Override
    public MarketAddress detailForAddress(Integer id) {
        // 生成一个相应对象做数据封装

//        MarketAddressExample marketAddressExample = new MarketAddressExample();
//        MarketAddressExample.Criteria criteria = marketAddressExample.createCriteria();
//        criteria.andIdEqualTo(id);
//        criteria.andDeletedEqualTo(false);
//        List<MarketAddress> marketAddresses = marketAddressMapper.selectByExample(marketAddressExample);
        MarketAddress marketAddress = marketAddressMapper.selectByPrimaryKey(id);


        return marketAddress;
    }


    /**
     * 修改地址信息
     * @param marketAddress
     * @return
     */
    @Override
    public int insertAdress(MarketAddress marketAddress) {
        marketAddress.setAddTime(new Date());
        int id = marketAddressMapper.insertSelective(marketAddress);
        Integer addressId = marketAddress.getId();
        System.out.println(addressId);
           return addressId;

    }

    @Override
    public int updateAddress(MarketAddress marketAddress) {
        int i = marketAddressMapper.updateByPrimaryKeySelective(marketAddress);
        Integer id = marketAddress.getId();
        return id;
    }

    /**
     * 删除地址
     * 逻辑删除，把delete设置为true
     * @param addressId
     */
    @Override
    public void deleteForAdress(Integer addressId) {
        MarketAddress marketAddress = new MarketAddress();
        marketAddress.setId(addressId);
        marketAddress.setDeleted(true);
        marketAddressMapper.updateByPrimaryKeySelective(marketAddress);

    }
}
